package com.tyut.controller;

import com.tyut.entity.User;
import com.tyut.service.UserService;
import com.tyut.vo.ResultJson;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 山田凉
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    UserService userService;
    @GetMapping("/list")
// 访问路径
    ResultJson list(Integer pageNo, Integer pageSize, String username ,Integer role ){
        return ResultJson.success(userService.list(pageNo, pageSize, username , role));
    }

    @PostMapping("/update")
    ResultJson update(User user){
        return ResultJson.success(userService.updateById(user),"修改成功");
    }

    @PostMapping("/del")
    ResultJson del(User user){
        return ResultJson.success(userService.updateById(user),user.getActive()?"还原成功" : "删除成功");
    }

    @GetMapping("/getById")
    ResultJson getById(Integer id){
        return ResultJson.success(userService.getById(id));
    }

    @GetMapping("/check")
    ResultJson check(String fieldName , String value , Integer id){
        return ResultJson.success(userService.check(fieldName,value,id));
    }

    @PostMapping("/login")
    ResultJson login(String username, String password){
        return ResultJson.success(userService.login(username,password));
    }

    @PostMapping("/register")
    ResultJson register(User user){
        return ResultJson.success(userService.register(user),"注册成功");
    }

    @PostMapping("/forget")
    ResultJson forget(String username , String phone, String password){
        return ResultJson.success(userService.forget(username ,phone,password),"修改成功");
    }

    @GetMapping("/getData")
    ResultJson getData(){
        return ResultJson.success(userService.getActive());
    }


    @GetMapping("/getUserMessage")
    ResultJson getUserMessage(Integer sessionId){
        return ResultJson.success(userService.getUserMessage(sessionId));
    }
    @PostMapping("/updateIcon")
    ResultJson postIcon( MultipartFile file , Integer sessionId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultJson.success(userService.updateIcon(file,sessionId),"修改成功");
    }
    @PostMapping("/updatePassword")
    ResultJson updatePassword(String oldPsw , String newPsw , Integer sessionId){
        return ResultJson.success(userService.updatePassword(oldPsw , newPsw  ,sessionId),"修改成功，请重新登录系统！");
    }
    @PostMapping("/updateEmail")
    ResultJson updateRole(String email , Integer sessionId){
        return ResultJson.success(userService.updateEmail(email , sessionId),"修改成功");
    }
    @PostMapping("/updateUsername")
    ResultJson updateEmail(String username , Integer sessionId){
        return ResultJson.success(userService.updateUsername(username , sessionId),"修改成功");
    }
    @PostMapping("/updatePhone")
    ResultJson updatePhone(String phone , Integer sessionId){
        return ResultJson.success(userService.updatePhone(phone , sessionId),"修改成功");
    }

    @GetMapping("/getApply")
    ResultJson getApply(Integer pageNo, Integer pageSize){
        return ResultJson.success(userService.getApply(pageNo, pageSize));
    }

}
